/*
 * @Description:
 * @Version: 1.0
 * @Autor: wangmeng
 * @Date: 2025-01-10 14:43:48
 * @LastEditors: wamgmeng
 * @LastEditTime: 2025-01-10 16:41:57
 */
export enum NavCmd {
    rotor = 1,
    rudder = 2, // 舵面
    sensor = 3,
    navigation = 4,
    control = 5,
    info = 6,
}

export interface INavParams {
    cmd: NavCmd;
    data: number;
}

export interface INavRotorParams extends INavParams {
    cmd: NavCmd.rotor;
}
export interface INavRudderParams extends INavParams {
    cmd: NavCmd.rudder;
}
export interface INavSensorParams extends INavParams {
    cmd: NavCmd.sensor;
}
export interface INavNavigationParams extends INavParams {
    cmd: NavCmd.navigation;
}
export interface INavControlParams extends INavParams {
    cmd: NavCmd.control;
}
export interface INavInfoParams extends INavParams {
    cmd: NavCmd.info;
}

export interface IRotorState {
    num: number;
    rotation_speed: number;
    pwm_fb: number;
    pwm_cmd: number;
    temp: number;
    status: number;
}

export interface IRotor {
    state_list: IRotorState[];
}

export class Rotor implements IRotor {
    state_list: IRotorState[] = [];

    // 修改构造函数支持多个数据
    constructor(data: IRotorState | IRotorState[]) {
        if (Array.isArray(data)) {
            this.state_list = data;
        } else {
            this.state_list.push(data);
        }
    }

    // 新增处理多个电机数据的静态方法
    static fromDataArray(params: number[]) {
        const states = params.map(param => ({
            num: param & 0x07,
            rotation_speed: (param >> 3) & 0x1f,
            pwm_fb: (param >> 8) & 0xff,
            pwm_cmd: (param >> 16) & 0xff,
            temp: (param >> 24) & 0xff,
            status: (param >> 31) & 0x01,
        }));

        return new Rotor(states);
    }

    // 保留原来的单个数据处理方法
    static fromData(param: number) {
        return new Rotor({
            num: param & 0x07,
            rotation_speed: (param >> 3) & 0x1f,
            pwm_fb: (param >> 8) & 0xff,
            pwm_cmd: (param >> 16) & 0xff,
            temp: (param >> 24) & 0xff,
            status: (param >> 31) & 0x01,
        });
    }
}


// 处理多个电机数据
const rawDataArray = [
    0b10101010_11110000_10101010_00000111, // 电机1数据
    0b10101010_11110000_10101010_00001000, // 电机2数据
    0b10101010_11110000_10101010_00001001  // 电机3数据
];

// 一次性创建包含三个电机状态的实例
const rotors = Rotor.fromDataArray(rawDataArray);

// 访问各个电机的状态
rotors.state_list.forEach((state, index) => {
    console.log(`电机 ${index + 1}:`);
    console.log('编号:', state.num);
    console.log('转速:', state.rotation_speed);
    console.log('PWM反馈值:', state.pwm_fb);
    console.log('PWM命令值:', state.pwm_cmd);
    console.log('温度:', state.temp);
    console.log('状态:', state.status);
    console.log('-------------------');
});


